package com.wcgksoft.controller.order;


import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.admin.Account;
import com.wcgksoft.entity.goods.Goods;
import com.wcgksoft.entity.order.Order;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.goods.GoodsService;
import com.wcgksoft.service.order.OrderService;
import com.wcgksoft.service.system.UserService;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author 御风
 * @version V1.0
 * @Package com.wcgksoft.controller.goods
 * @Description 购物订单
 * @date 2020/5/12 16:11
 * @ClassName GoodsController
 */
@Controller
@RequestMapping("financeorder/list")
public class financeorderController {

  @Autowired
  private GoodsService goodsService;
  @Autowired
  private OrderService orderService;

  @Autowired
  private UserService userService;

  @ResponseBody
  @RequestMapping(value = "findallorder")
  public Layui findallorder(@RequestParam(value = "page", required = false) Integer page,
                            @RequestParam(value = "limit", required = false) Integer limit) {

    DetachedCriteria criteria = DetachedCriteria.forClass(Order.class);

    criteria.add(Restrictions.eq("deletetype", 0));
    criteria.addOrder(org.hibernate.criterion.Order.desc("buyTime"));
    List<Order> list = orderService.findByConditions(criteria, page, limit);
    return Layui.data(0, "查询成功", orderService.getTotalCount(), list);
  }

  @ResponseBody
  @RequestMapping(value = "find")
  public Layui findAll() {
    DetachedCriteria criteria = DetachedCriteria.forClass(Goods.class);
    List<Goods> list = goodsService.findByConditions(criteria);
    return Layui.data(0, "查询成功", list);
  }

  @ResponseBody
  @RequestMapping(value = "findgood")
  public Layui findgood(@RequestParam(value = "gid", required = false) Integer gid) {
    Goods goods = goodsService.findById(gid);
    return Layui.data(0, "查询成功", goods);
  }

  @ResponseBody
  @RequestMapping(value = "update")
  public Layui update(HttpServletRequest request,
                      @RequestParam(value = "data", required = false) String data) throws Exception {
    JSONObject param = JSONObject.fromObject(data);
    if (param == null) {
      return Layui.data(1, "请求格式有误", 0, null);
    } else {
      Order order = (Order) JSONObject.toBean(JSONObject.fromObject(data), Order.class);
      order.setOrderNo(getOrderIdByTime());
      Account account = (Account) SecurityUtils.getSubject().getSession().getAttribute("om_user");
      order.setAgentId(account.getId());
      order.setBuyTime(new Date());
      order.setStatus(1);
      order.setDeletetype(0);
      order.setAccountname(account.getName());
      orderService.saveOrUpdate(order);
      return Layui.data(0, "保存成功", 1, Layui.addList(order));
    }
  }

  /**
   * 删除
   *
   * @param id
   * @param
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "confirm")
  @Transactional
  public Layui delete(@RequestParam(value = "id") Integer id) {
    System.out.println("id" + id);
    Order byId = orderService.findById(id);
    byId.setStatus(2);
    orderService.saveMerge(byId);

    return Layui.data(1, "保存成功", 1, Layui.addList(byId));
  }


  /**
   * 删除
   *
   * @param id
   * @param
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "refuse")
  @Transactional
  public Layui refuse(@RequestParam(value = "id") Integer id,
                      @RequestParam(value = "code") Integer code,
                      @RequestParam(value = "reason1") String reason1) {
    System.out.println("id" + id);
    Order byId = orderService.findById(id);
    SysUser sysUser = byId.getSysUser();
    //设置不显示
    sysUser.setIsView(2);
    sysUser.setIsCross(2);
    userService.saveMerge(sysUser);
    byId.setStatus(code);
    byId.setReason1(reason1);
    orderService.saveMerge(byId);
    return Layui.data(1, "操作成功", 1, Layui.addList(byId));
  }





  public static String getOrderIdByTime() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String newDate = sdf.format(new Date());
    String result = "";
    Random random = new Random();
    for (int i = 0; i < 3; i++) {
      result += random.nextInt(10);
    }
    return newDate + result;
  }
}
